package gui;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;

import javax.swing.JPanel;

import common.Constants;

public class SunDisplay extends JPanel {
  private static final long serialVersionUID = 1L;

  private int pathLength;
  private float pixelsPerDegree;
  private int lineOffsetY = 10;
  private int dimHeight = lineOffsetY+10;
  private int sunDiameter = 10;
  private Color sunColor = Color.yellow;
  private float degreePosition = 360f;
  private int pixelPosition;
  
  public SunDisplay(int pathLength) {
    drawSunPath(pathLength);
    moveSunPosition(Constants.SUN_POSITION);
  }
  
  public void drawSunPath(int pathLength) {
    this.pathLength = pathLength;
    pixelsPerDegree = pathLength / 360f;    
    
    Dimension dim = new Dimension(pathLength, dimHeight);
    setPreferredSize(dim);
    setMaximumSize(dim);
  }
  
  public void paint(Graphics g) {
    //draw the path
    g.setColor(Color.black);
    g.drawLine(0, lineOffsetY, pathLength, lineOffsetY);
    
    //draw the sun
    g.setColor(sunColor);
    g.fillOval(pixelPosition, lineOffsetY/2, sunDiameter, sunDiameter);
  }
  
  public void moveSunPosition(float degrees) {
    degreePosition -= degrees;
    if(degreePosition < 0) {
      degreePosition += 360;
    }
    float pos = pixelsPerDegree * degreePosition;
    pixelPosition = (int)(pos - sunDiameter/2);
  }
  
  public void reset() {
    degreePosition = 0f;
    pixelPosition = 0;
    moveSunPosition(Constants.SUN_POSITION);
    paint(getGraphics());
  }

}
